//
//  OrderViewController.m
//  FD
//
//  Created by Yasin on 2018/10/10.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  我的订单主控制
//4:30 ~ 5:30

#import "OrderViewController.h"
#import "WSOrderViewController.h" //外送订单
#import "ZTOrderViewController.h" //自提订单
#import "YuYueOrderViewController.h" //预约订单
#import "RBTitleButton.h"

@interface OrderViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIView *titleView;
/**标题按钮底部指示器*/
@property (nonatomic, weak) UIView *indicatorView;
/**当前选中的标题按钮*/
@property (nonatomic, weak) RBTitleButton *selectedTitleButton;
/**scrollView */
@property (nonatomic, weak) UIScrollView *scrollView;
@end

@implementation OrderViewController

+ (instancetype)shareOrderViewController {
    static OrderViewController *share = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[OrderViewController alloc] init];
    });
    return share;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupView];
    
    [self setChildVcViewControllers];
    
    [self setupScrollView];
    
    [self addChildVcView]; // 默认已经来就创建全部自控制器。
    
    //注意调用顺序，不然会被盖住的
    [self setupTitleView];
    
//    FDLog(@"%@",self.titleView.subviews);
    FDLog(@"来了");
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMenDianYuyueVC) name:kShowMenDianYuYueVCNotification object:nil];
}

- (void)showMenDianYuyueVC{
    RBTitleButton *btn = self.titleView.subviews[2];
    [btn.titleLabel sizeToFit];
    self.indicatorView.rb_width = btn.rb_width;
    self.indicatorView.rb_center_x = btn.rb_center_x;
    [self titleClick:btn];
}
- (void)dealloc {
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kShowMenDianYuYueVCNotification object:nil];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


- (void)setupView {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSLocalizedString(My_MyOrder, @"我的订单");
}

- (void)setupTitleView
{
    // 标题栏
    UIView *titleView = [[UIView alloc] init];

    titleView.backgroundColor = [UIColor whiteColor];
    titleView.frame = CGRectMake(0, FD_NavH, self.view.rb_width, 35);
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    NSString *wsStr = NSLocalizedString(Home_Delivery, @"外送");
    NSString *ztSTr = NSLocalizedString(Home_SelfLifting, @"自提");
    NSString *yuyueStr = NSLocalizedString(Home_Reservation, @"门店预约");

    NSArray *titleArr = @[wsStr,ztSTr,yuyueStr];
    CGFloat btnW = titleView.rb_width / titleArr.count;
    CGFloat btnH = titleView.rb_height;
    // 添加标题
    for (NSUInteger i =0; i < titleArr.count ; i++) {
        // 创建
        RBTitleButton *titleButton = [RBTitleButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        [titleView addSubview:titleButton];
        
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:(UIControlEventTouchUpInside)];
        
        // 设置标题
        [titleButton setTitle:titleArr[i] forState:(UIControlStateNormal)];
        
        // frame
        titleButton.frame = CGRectMake(i*btnW, 0, btnW, btnH);
    }
    
    // 按钮的选中颜色
    RBTitleButton *firstTitleButton = titleView.subviews.firstObject;
    
    // 底部指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor]; //红色的线
    indicatorView.rb_height = 2;
    indicatorView.rb_y = titleView.rb_height - indicatorView.rb_height;
    [titleView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    // 提前计算好按钮label长度
    [firstTitleButton.titleLabel sizeToFit];
    self.indicatorView.rb_width = firstTitleButton.titleLabel.rb_width; // 指示器宽度=btn.titleLabel的宽度
    self.indicatorView.rb_center_x = firstTitleButton.rb_center_x;
    
    // 设置默认选中按钮
    [self titleClick:firstTitleButton];
}
- (void)titleClick:(RBTitleButton *)btn
{
    self.selectedTitleButton.selected = NO;
    btn.selected = YES;
    self.selectedTitleButton = btn;
    
    // 指示器
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.rb_width = btn.titleLabel.rb_width; // 指示器宽度=btn.titleLabel的宽度
        self.indicatorView.rb_center_x = btn.rb_center_x;
    }];
    
    // 让scrollView滚动到对应的位置
    CGPoint offset = self.scrollView.contentOffset;
    offset.x  = btn.tag * _scrollView.rb_width;
    [self.scrollView setContentOffset:offset animated:YES];
}


- (void)setChildVcViewControllers
{
    WSOrderViewController *wsVC = [[WSOrderViewController alloc] init];
    [self addChildViewController:wsVC];
    
    ZTOrderViewController *ztVC = [[ZTOrderViewController alloc] init];
    [self addChildViewController:ztVC];
    
    YuYueOrderViewController *yuyueVC = [[YuYueOrderViewController alloc] init];
    [self addChildViewController:yuyueVC];
}

#pragma mark - 添加子控制器view到scrollView上
- (void)addChildVcView
{
    // 子控制器索引 index
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.rb_width;
    
    // 取出子控制器
    UIViewController *childVc = self.childViewControllers[index];
    
    // 优化代码
    //    if (childVc.view.superview) return; // 如果supView有值，说明已经被添加到superView上了,就不重复添加
    //    if (childVc.view.window) return; // 如果window有值，说明已经被加到window上
    if ([childVc  isViewLoaded]) return; //

    childVc.view.frame = self.scrollView.bounds;
    
    [self.scrollView addSubview:childVc.view];
}

- (void)setupScrollView
{
    // 作用:不允许自动调整scrollView的内边距,让scrollView全屏
    self.automaticallyAdjustsScrollViewInsets =  NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.pagingEnabled = YES;
    //    scrollView.showsVerticalScrollIndicator = NO;
    //    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    NSUInteger count = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(count * scrollView.rb_width, 0);  //禁止scrollView上下滚动
}

#pragma mark - <UIScrollViewDelegate>
/**
 * 让scrollView产生滚动动画,动画结束的时候回调用这个方法
 * 前提:必须使用setContentOffset:animated方法 或者 scrollRectVisible:animated:方法
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    [self addChildVcView];
}

/**
 * 人为拖拽,↔️左右滑动,会触发这个方法
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //    RBNSLogFunction;
    
    NSUInteger index = scrollView.contentOffset.x / scrollView.rb_width;
    
    // 左右拖拽,更新顶部标题选中状态
    RBTitleButton *titleButton = self.titleView.subviews[index];
    
    // 默认情况下:所有view的tag都是0
    // 当tag==0时,viewWithTag 方法返回的就是 self.titleView
    //    RBTitleButton *titleButton = [self.titleView viewWithTag:0];
    
    [self titleClick:titleButton];
    
    // 子控制器view添加到scrollView上
    [self addChildVcView];
}


#pragma mark - --2019-1-10
- (void)showYuYueOrderListVC {
    
    FDLog(@"%@",self.titleView.subviews);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        RBTitleButton *titleButton = self.titleView.subviews[2];
        [self titleClick:titleButton];
    });
}

@end
